/**
 * 
 */
package ua.lviv.franko.mlsFunctions.twoDimension.weightFunctions;

import org.jzy3d.maths.Range;
import org.jzy3d.plot3d.builder.Mapper;

import ua.lviv.franko.mlsFunctions.oneDimension.weightFunctions.CubicSpline;
import ua.lviv.franko.mlsFunctions.oneDimension.weightFunctions.IWeightFunction;
import ua.lviv.franko.mlsFunctions.twoDimension.Particle;
import ua.lviv.franko.visualisation.Visualise3D;

/**
 * @author marko
 * 
 */
public class SquareSupport implements IWeightFunction2D {

	protected IWeightFunction	func;

	public SquareSupport(IWeightFunction func) {
		this.func = func;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see mlsFunctions.twoDimension.weightFunctions.IWeightFunction2D#calculate (double, double)
	 */
	@Override
	public double calculate(double x, double y, double xi, double yi, double rho) {
		return func.calculate(x, xi, rho) * func.calculate(y, yi, rho);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see mlsFunctions.twoDimension.weightFunctions.IWeightFunction2D# calculateDerivatyX(double, double)
	 */
	@Override
	public double calculateDerivatyX(double x, double y, double xi, double yi, double rho) {
		return func.calculateDerivaty(x, xi, rho) * func.calculate(y, yi, rho);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see mlsFunctions.twoDimension.weightFunctions.IWeightFunction2D# calculateDerivatyY(double, double)
	 */
	@Override
	public double calculateDerivatyY(double x, double y, double xi, double yi, double rho) {
		return func.calculate(x, xi, rho) * func.calculateDerivaty(y, yi, rho);
	}

	@Override
	public boolean intersectedSupport(Particle p1, Particle p2) {
		// if (Math.abs(this.calculate(p1.getXi(), p1.getYi(), p2.getXi(),
		// p2.getYi(), p1.getRho()))>0.000000000001) {
		// return true;
		// }else{
		// if (Math.abs(this.calculate(p1.getXi(), p1.getYi(), p2.getXi(),
		// p2.getYi(), p2.getRho()))>0.000000000001) {
		// return true;
		// }
		// }
		// return false;
		double ax = Math.max(p1.getXi() - p1.getRho(), p2.getXi() - p2.getRho());
		double bx = Math.min(p1.getXi() + p1.getRho(), p2.getXi() + p2.getRho());
		double ay = Math.max(p1.getYi() - p1.getRho(), p2.getYi() - p2.getRho());
		double by = Math.min(p1.getYi() + p1.getRho(), p2.getYi() + p2.getRho());
		//
		boolean v = ((bx - ax > 0.0000000001) && (by - ay > 0.0000000001));
		// boolean v2 =
		// ((p1.lengthTo(p2)-(p1.getRho()+p2.getRho())/Math.sqrt(2.0))<0.00000001);
		return v;// ((p1.lengthTo(p2)-(p1.getRho()+p2.getRho())/Math.sqrt(2.0))<0.00000001);
	};

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		final SquareSupport ffff = new SquareSupport(new CubicSpline());

		Visualise3D.show(new Mapper() {

			@Override
			public double f(double x, double y) {
				return ffff.calculate(x, y, 0.5, 0.5, 0.5);// (x, y);
			}
		}, new Range(0.0, 1.0), new Range(0.0, 1.0));

	}

}
